package com.anrongtec.zcpt.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 作者：liuhui on 2017/2/13 14:11
 * 邮箱：liu594545591@126.com
 * 描述：PreferenceUtils
 */

public class PreferenceUtils {

    /**
     * 保存Preference的name
     */
    private static SharedPreferences mSharedPreferences;
    private static PreferenceUtils mPreferenceUtils;
    private static SharedPreferences.Editor editor;
    private static Context context;

    private static final String DEFAULT_NAME = "default_name";

    private PreferenceUtils(Context cxt, String name) {
        // mSharedPreferences =
        // cxt.getSharedPreferences(DemoApplication.getInstance().getUser()+PREFERENCE_NAME,
        // Context.MODE_PRIVATE);
        mSharedPreferences = cxt.getSharedPreferences(name,
                Context.MODE_PRIVATE);
        // PreferenceUtils.getInstance().setContext().setname().
        // PreferenceUtils getInstance( cxt, preName);
        // util extends PreferenceUtils
        //
        // // PreferenceUtils..getInstance().setContext.saveName("liuhui");
    }


    /**
     * 单例模式，获取instance实例
     *
     * @param cxt
     * @return
     */
    public static PreferenceUtils getInstance(Context cxt) {
        return getInstance(cxt, DEFAULT_NAME);
    }

    /**
     * 单例模式，获取instance实例
     *
     * @param cxt
     * @param preName
     * @return
     */
    public static PreferenceUtils getInstance(Context cxt, String preName) {
        context = cxt.getApplicationContext();
        if (mPreferenceUtils == null) {
            mPreferenceUtils = new PreferenceUtils(context, preName);
        }
        editor = mSharedPreferences.edit();
        return mPreferenceUtils;
    }

    /**
     * 保存
     * @param value
     * @param key
     */
    public void save(String key, String value){
        editor.putString(key, value);
        editor.commit();
    }
    public void save(String key, boolean value){
        editor.putBoolean(key, value);
        editor.commit();
    }
    public void save(String key, int value){
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取
     * @param key
     * @return
     */
    public String getString(String key){
        return mSharedPreferences.getString(key, "");
    }
    public boolean getBoolean(String key){
        return mSharedPreferences.getBoolean(key, true);
    }
    public int getInt(String key){
        return mSharedPreferences.getInt(key, 0);
    }

    //
    // /**
    // * 设置当前用户
    // */
    // public void setCurrentUser(String userName) {
    // editor.putString(SHARED_KEY_SETTTING_USER_CURRENT, userName);
    // editor.commit();
    // }
    //
    // /**
    // * 获取当前用户
    // *
    // * @param userName
    // * @return
    // */
    // public String getCurrentUser() {
    // return mSharedPreferences.getString(SHARED_KEY_SETTTING_USER_CURRENT,
    // "");
    // }
}

